﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using JYH.Business;
using JYH.Data;
using JYH.DataAccess;
using JYH.Framework;
using JYH.Model;

namespace JYH.Web.MES
{
    public class PageBase:AuthenticationPage
    {

        #region 清空浏览器缓存
        private bool __isClearPageCache = false;

        /// <summary>
        /// 是否清空客户端浏览器缓存，默认不清空
        /// </summary>
        protected virtual bool IsClearPageCache
        {
            get { return __isClearPageCache; }
        }
        /// <summary>
        /// 清空客户端浏览器缓存
        /// </summary>
        private void ClearPageCache()
        {
            Response.Buffer = true;
            Response.Expires = 0;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.AddHeader("cache-control", "private");
            Response.CacheControl = "no-cache";

        }
        #endregion

        #region MenuID属性
        /// <summary>
        /// 设置MenuID的Session
        /// </summary>
        public void SetMenuID()
        {
            string menuID = Request.QueryString["menuID"];
            if (!string.IsNullOrEmpty(menuID))
            {
                HttpContext.Current.Session["MenuID"] = menuID;
            }
            else
            {
                menuID = Request.QueryString["MenuID"];
                if (!string.IsNullOrEmpty(menuID))
                {
                    HttpContext.Current.Session["MenuID"] = menuID;
                }
                else
                {
                    HttpContext.Current.Session["MenuID"] = Guid.NewGuid().ToString();
                }
            }
            MenuID = HttpContext.Current.Session["MenuID"].ToString();
        }
        public string MenuID
        {
            get;set;
        }
        #endregion

        #region 参数属性
        /// <summary>
        /// 传递参数ID
        /// </summary>
        protected string KeyID
        {
            get
            {
                if (string.IsNullOrEmpty(Request.Params["ID"]))
                {
                    return Guid.NewGuid().ToString();
                }
                else
                {
                    return Request.Params["ID"].ToString();
                }
            }
        }
        #endregion

        #region 显示明细数据
        /// <summary>
        /// 载入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="pnlContrainer"></param>
        protected virtual void LoadData<T>(T entity, System.Web.UI.Control pnlContainer) where T : class
        {
            if (entity != null && pnlContainer != null)
            {
                var e = new ReflectHelper<T>();
                e.BindDataToControl(entity, pnlContainer);

            }
        }

        protected virtual void SaveData<T>(T entity, System.Web.UI.Control pnlContainer) where T : class
        {
            if (entity != null && pnlContainer != null)
            {
                var e = new ReflectHelper<T>();
                e.AssignEntity(entity, pnlContainer);
            }
        }
        #endregion

        protected override void OnPreInit(EventArgs e)
        {
            if (IsClearPageCache)
                ClearPageCache();
            base.OnPreInit(e);
        }

        /// <summary>
        /// 清除页面控件内容
        /// </summary>
        /// <param name="ctrl"></param>
        protected virtual void ClearControl(System.Web.UI.Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                if (c.HasControls())
                {
                    ClearControl(c);
                }
                else
                {
                    if (c.GetType() == typeof(TextBox)) ((TextBox)c).Text = "";
                    if (c.GetType() == typeof(DropDownList)) ((DropDownList)c).SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// 是否加载JS/CSS文件
        /// </summary>
        protected virtual bool IsLoad
        {
            get { return true; }
        }

        protected override void OnPreLoad(EventArgs e)
        {
           

        
            SetMenuID();
            base.OnPreLoad(e);
         //   ButtonControl();

        }

        protected void ButtonControl()
        {
            try
            {
                //if (ConfigHelper.CheckConnectionStringKey("DBCONN"))
                //{
                //    myDatabase = new Database("DBCONN");
                //}
                //Sql sql = new Sql();
                //sql.Append(@";Select  SysFunctionInfo.* From SysFunctionInfo
                //                    Inner JOin SysRoleMenuFunction ON SysFunctionInfo.FunctionID=SysFunctionInfo.FunctionID
                //                    Inner JOin SysRoleUser ON SysRoleMenuFunction.RoleID=SysRoleUser.RoleID
                //                    Inner JOIn SysUserInfo  ON SysRoleUser.UserID=SysUserInfo.UserID
                //                    ");
                //string UserNO = string.Empty;
                //if (CurrentUser != null)
                //{
                //    UserNO = CurrentUser.UserNO;
                //}
                //sql.Append("Where SysUserInfo.UserNO='" + UserNO + "'");
                //sql.Append("Where SysRoleMenuFunction.MenuID='" + this.MenuID + "'");
                //sql.OrderBy("SysFunctionInfo.OrdIndex");
                //List<Button> FunctionInfos = new List<Button>();
                //FunctionInfos = myDatabase.Fetch<SysFunctionInfo>(sql);
               

            }
            catch 
            {
                
            }
        }

        protected void SetControl(Control c, List<SysButtonInfo> Buttons)
        {
            Control Con =  GetControl("AddNew",c);
            if (Con != null)
            {
                
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (CurrentUser == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
        }
        public string GetDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }
        #region 加载JS与CSS文件
        /// <summary>
        /// 加载JS文件
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="fileName"></param>
        public void AppendJavascriptFile(int idx, string fileName)
        {
            HtmlGenericControl _jsFile = new HtmlGenericControl("script");
            _jsFile.Attributes["type"] = "text/javascript";
            _jsFile.Attributes["src"] = ResolveUrl("~/") + fileName;
            Page.Header.Controls.AddAt(idx, _jsFile);
        }
        /// <summary>
        /// 加载CSS文件
        /// </summary>
        /// <param name="fileName"></param>
        public void AppendCssFile(string fileName)
        {
            HtmlGenericControl _cssFile = new HtmlGenericControl("link");
            _cssFile.Attributes["type"] = "text/css";
            _cssFile.Attributes["rel"] = "stylesheet";
            _cssFile.Attributes["href"] = ResolveUrl("~/") + fileName;
            Page.Header.Controls.AddAt(0, _cssFile);
        }
        #endregion


        public void SetValueToControl(string ctrlName, string value, Control control)
        {
            if (control.ID == null) return;

            var _control = GetControl(ctrlName, control);
            if (_control == null) return;
            var _ctrlType = _control.GetType();
            if (_ctrlType == typeof(System.Web.UI.WebControls.Label))
                if (!string.IsNullOrEmpty(value))
                    ((System.Web.UI.WebControls.Label)_control).Text = value;
            if (_ctrlType == typeof(Button))
                if (!string.IsNullOrEmpty(value))
                    ((Button)_control).Text = value;
            if (_ctrlType == typeof(LinkButton))
                if (!string.IsNullOrEmpty(value))
                    ((LinkButton)_control).Text = value;
            if (_ctrlType == typeof(HyperLink))
                if (!string.IsNullOrEmpty(value))
                    ((HyperLink)_control).Text = value;

        }

        List<string> StrConn = new List<string>();
        public Control GetControl(string ctrlName, Control control)
        {
            Control obj = null;
            foreach (Control ctr in control.Controls)
            {

                if (string.Equals(ctr.ID, ctrlName))
                {
                    obj = ctr;
                    break;
                }
                else
                {
                    StrConn.Add(ctr.ID+"|"+ctr.ClientID+"|");
                    if (ctr.HasControls())
                    {
                        obj = GetControl(ctrlName, ctr);
                    }
                }
                if (obj != null)
                {
                    break;
                }

            }
            return obj;

        }
    }
}
